/*
 * Created on 2005-1-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package nc.tb.ctl;
import java.util.ArrayList;
import java.util.Iterator;

import nc.tb.app.CommonPlan;
import nc.tb.app.PlanInfo;
import nc.tb.cube.model.MetaPlan;
import nc.tb.cube.model.Plan;
import nc.tb.pub.BusiObj;
import nc.tb.pub.IDimType;
import nc.tb.pub.cache.NtbCacheImpl;
import nc.tb.pub.exception.ExpiredCopyCheckException;
import nc.tb.pub.exception.NtbRException;
import nc.tb.pub.services.EditControlServiceGetter;
import nc.tb.pub.services.NtbSheetObjectServiceGetter;
import nc.tb.pub.util.NtbLogger;
import nc.tb.ufo.sheet.EditControlDelegate;
import nc.tb.ufo.sheet.PlanLockRtn;
import nc.vo.pub.BusinessException;
/**
 * @author tzj
 * 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 */
public class EditControlCTL extends CTLBase {
	public static void checkOut(Plan so) throws BusinessException {
		try {
			CTLBase.validateISO(so);
			EditControlDelegate ed = EditControlServiceGetter.getIEditControl().checkOut(so.getBusiObj(), so.getEditControlDelegate(), getLoginUserName());
			if (ed != null) {
				so.setEditControlDelegate(ed);
			}
		} catch (ExpiredCopyCheckException ece) {
			String pk = ece.getPrimaryKey();
			try {
					so.fireDataCellChanged();
				//}
				so.setEditControlDelegate(ece.getEditDelegate());
			} catch (Exception e) {
				NtbLogger.error(e);
			}
			throw ece;
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.error(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void checkOutArray(CommonPlan[] plans) throws BusinessException {
		if (plans == null || plans.length == 0)
			return;
		try {
			ArrayList<PlanInfo> checkedBoList = new ArrayList<PlanInfo>();
			ArrayList<EditControlDelegate> checkedEdList = new ArrayList<EditControlDelegate>();
			for (int i=0; i<plans.length; i++) {
				CTLBase.validateISO(plans[i]);
				checkedBoList.add(plans[i].getPlanInfo());
				checkedEdList.add(plans[i].getEditControlDelegate());
			}
			PlanLockRtn[] rtns = EditControlServiceGetter.getIEditControl().checkOutArray(
					checkedBoList.toArray(new PlanInfo[0]), checkedEdList.toArray(new EditControlDelegate[0]), getLoginUserName());
			if (rtns != null) {
				for (int i=0; i<rtns.length; i++) {
					if (rtns[i].isTimeOut()) {
						plans[i].fireDataCellChanged();
					}
					EditControlDelegate ed = rtns[i].getEditControlDelegate();
					if (ed != null)
						plans[i].setEditControlDelegate(ed);
				}
			}
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.error(e);
			throw new BusinessException(e.getMessage());
		}
	}

	public static void checkIn(Plan so) throws BusinessException {
		try {
			if (so.getPrimaryKey() != null)
				CTLBase.validateISO(so);
			//			EditControlDelegate ed = EditControlBOClientGetter.getIEditControl().checkOut(so.getBusiObj(), so.getEditControlDelegate(), getLoginUserName());
			//			if (ed != null)
			//				so.setEditControlDelegate(ed);
			//if(!b) expired.
			//ca.invalidate(iso);
			if (so instanceof MetaPlan) {
				StyleSheetCTL.saveCube((MetaPlan) so);
			} else if (so instanceof CommonPlan) {
				PlanCTL.savePlan((CommonPlan) so);
			} /*else if (so instanceof NtbAssumption) {
				AssumptionCTL.saveAssumption((NtbAssumption) so);
			}*/
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void checkInArray(CommonPlan[] plans) throws BusinessException {
		try {
			for (int i=0; i<plans.length; i++) {
				if (plans[i].getPrimaryKey() != null)
					CTLBase.validateISO(plans[i]);
			}
			PlanCTL.savePlanArray(plans);
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}

	public static void checkOutPlanByView(Plan so,String pkView)throws BusinessException{
		debug("check out "+so.getObjname()+" by view "+pkView);
		try {
			CTLBase.validateISO(so);
			
			EditControlDelegate ed = EditControlServiceGetter.getIEditControl().checkOutPlanByView(so.getBusiObj(), pkView, so.getEditControlDelegate(), getLoginUserName());
			if (ed != null) {
				so.setEditControlDelegate(ed);
			}
		} catch (ExpiredCopyCheckException ece) {
			String pk = ece.getPrimaryKey();
			try {
					so.fireDataCellChanged();
				//}
				so.setEditControlDelegate(ece.getEditDelegate());
			} catch (Exception e) {
				NtbLogger.error(e);
			}
			throw ece;
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.error(e);
			throw new BusinessException(e.getMessage());
		}

//		EditControlDelegate ed = EditControlServiceGetter.getIEditControl().checkOutPlanByView(so.getBusiObj(), pkView, so.getEditControlDelegate(), getLoginUserName());
//		if (ed != null) {
//			so.setEditControlDelegate(ed);
//		}
	}
	public static boolean checkOutPlanArrayByView(CommonPlan[] plans, String[] pk_cubeViews)throws BusinessException{
		if (plans == null || plans.length == 0 || pk_cubeViews == null || pk_cubeViews.length == 0 || pk_cubeViews.length != plans.length)
			return false;
		try {
			PlanInfo[] checkedBoList = new PlanInfo[plans.length];
			EditControlDelegate[] checkedEdList = new EditControlDelegate[plans.length];
			for (int i=0; i<plans.length; i++) {
				CTLBase.validateISO(plans[i]);
				checkedBoList[i] = plans[i].getPlanInfo();
				checkedEdList[i] = plans[i].getEditControlDelegate();
			}
			PlanLockRtn[] rtns = EditControlServiceGetter.getIEditControl().checkOutPlanArrayByView(
					checkedBoList, pk_cubeViews, checkedEdList, getLoginUserName());
			boolean isTimeout = false;
			if (rtns != null) {
				for (int i=0; i<rtns.length; i++) {
					if (rtns[i].isTimeOut()) {
						plans[i].fireDataCellChanged();
						isTimeout = true;
					}
					EditControlDelegate ed = rtns[i].getEditControlDelegate();
					if (ed != null)
						plans[i].setEditControlDelegate(ed);
				}
			}
			return isTimeout;
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.error(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void checkInPlanByView(Plan so,String pkView)throws BusinessException{
		debug("check in "+so.getObjname()+" by view "+pkView);
		EditControlDelegate ed = EditControlServiceGetter.getIEditControl().checkInPlanByView(so.getBusiObj(), pkView, so.getEditControlDelegate(), getLoginUserName());
		if (ed != null) {
			so.setEditControlDelegate(ed);
		}
		
	}
	public static void checkInPlanArrayByView(CommonPlan[] plans, String[] pk_cubeViews) throws BusinessException{
		if (plans == null || plans.length == 0 || pk_cubeViews == null || pk_cubeViews.length == 0 || pk_cubeViews.length != plans.length)
			return;
		try {
			PlanInfo[] checkedBoList = new PlanInfo[plans.length];
			EditControlDelegate[] checkedEdList = new EditControlDelegate[plans.length];
			for (int i=0; i<plans.length; i++) {
				checkedBoList[i] = plans[i].getPlanInfo();
				checkedEdList[i] = plans[i].getEditControlDelegate();
			}
			PlanLockRtn[] rtns = EditControlServiceGetter.getIEditControl().checkInPlanArrayByView(
					checkedBoList, pk_cubeViews, checkedEdList, getLoginUserName());
			if (rtns != null) {
				for (int i=0; i<rtns.length; i++) {
					EditControlDelegate ed = rtns[i].getEditControlDelegate();
					if (ed != null) {
						ed.setCheckedOut(false);
						plans[i].setEditControlDelegate(ed);
					}
				}
			}
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void checkIn(Plan so, boolean isAutoSave) throws BusinessException {
		if (isAutoSave)
			checkIn(so);
		else {
			try {
				if (so.getPrimaryKey() != null)
					CTLBase.validateISO(so);
				
				EditControlDelegate ed = NtbSheetObjectServiceGetter.getINtbSheetObjectBO().checkIn(so);
				if (ed != null)
					so.setEditControlDelegate(ed);
				
			} catch (BusinessException e) {
				throw e;
			} catch (NtbRException e) {
				throw e;
			} catch (Exception e) {
				NtbLogger.printException(e);
				throw new BusinessException(e.getMessage());
			}
		}
	}
	public static void checkInArray(CommonPlan[] plans, boolean isAutoSave) throws BusinessException {
		if (plans == null || plans.length == 0)
			return;
		if (isAutoSave)
			checkInArray(plans);
		else {
			try {
				ArrayList<PlanInfo> checkedBoList = new ArrayList<PlanInfo>();
				ArrayList<EditControlDelegate> checkedEdList = new ArrayList<EditControlDelegate>();
				for (int i=0; i<plans.length; i++) {
					if (plans[i].getPrimaryKey() != null)
						CTLBase.validateISO(plans[i]);
					checkedBoList.add(plans[i].getPlanInfo());
					checkedEdList.add(plans[i].getEditControlDelegate());
				}
				PlanLockRtn[] rtns = EditControlServiceGetter.getIEditControl().checkInArray(
						checkedBoList.toArray(new PlanInfo[0]), checkedEdList.toArray(new EditControlDelegate[0]), getLoginUserName());
				if (rtns != null) {
					for (int i=0; i<rtns.length; i++) {
						EditControlDelegate ed = rtns[i].getEditControlDelegate();
						if (ed != null) {
							ed.setCheckedOut(false);
							plans[i].setEditControlDelegate(ed);
						}
					}
				}
			} catch (BusinessException e) {
				throw e;
			} catch (NtbRException e) {
				throw e;
			} catch (Exception e) {
				NtbLogger.printException(e);
				throw new BusinessException(e.getMessage());
			}
		}
	}
	

	public static void undoCheckOut(Plan so) throws BusinessException {
		try {
			CTLBase.validateISO(so);
			EditControlDelegate ed = EditControlServiceGetter.getIEditControl().undoCheckOut(so.getBusiObj(), so.getEditControlDelegate(), getLoginUserName());
			if (so.isChanged()){
				//so.getActiveModel().unloadCellsList();
				so.fireDataCellChanged();
				if(so instanceof CommonPlan){
					Iterator it = NtbCacheImpl.getCache().listCacheObjects();
					while(it.hasNext()){
						Object o=it.next();
						if(o instanceof CommonPlan){
//							((Plan)o).getHmFormulaChains().clear();
							((CommonPlan)o).clearFormulaChains();
						}
					}
				}
			}
			if (ed != null)
				so.setEditControlDelegate(ed);
			if(so instanceof CommonPlan){
				Plan[] remoteQueryCopy = NtbSheetObjectServiceGetter.getINtbSheetObjectBO().queryByCond(IDimType.PLAN, " pk_obj='" + so.getPrimaryKey() + "'");
				if(remoteQueryCopy != null && remoteQueryCopy.length == 1)
				so.getBusiObj().copyFrom(remoteQueryCopy[0].getBusiObj());
			}else if(so instanceof MetaPlan){
				Plan[] remoteQueryCopy = NtbSheetObjectServiceGetter.getINtbSheetObjectBO().queryByCond(IDimType.STYLESHEET, " pk_obj='" + so.getPrimaryKey() + "'");
				if(remoteQueryCopy != null && remoteQueryCopy.length == 1)
				so.getBusiObj().copyFrom(remoteQueryCopy[0].getBusiObj());
				
			}
			
			//if(!b) expired.
			//ca.invalidate(iso);
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void undoCheckOutArray(CommonPlan[] plans) throws BusinessException {
		if (plans == null || plans.length == 0)
			return;
		try {
			ArrayList<PlanInfo> checkedBoList = new ArrayList<PlanInfo>();
			ArrayList<EditControlDelegate> checkedEdList = new ArrayList<EditControlDelegate>();
			for (int i=0; i<plans.length; i++) {
				CTLBase.validateISO(plans[i]);
				checkedBoList.add(plans[i].getPlanInfo());
				checkedEdList.add(plans[i].getEditControlDelegate());
			}
			PlanLockRtn[] rtns = EditControlServiceGetter.getIEditControl().undoCheckOutArray(
					checkedBoList.toArray(new PlanInfo[0]), checkedEdList.toArray(new EditControlDelegate[0]), getLoginUserName());
			if (rtns != null) {
				NtbCacheImpl cache = NtbCacheImpl.getCache();
				for (int i=0; i<rtns.length; i++) {
					if (plans[i].isChanged()) {
						plans[i].fireDataCellChanged();
						if(plans[i] instanceof CommonPlan){
							Iterator it = cache.listCacheObjects();
							while(it.hasNext()){
								Object o=it.next();
								if(o instanceof CommonPlan){
//									((Plan)o).getHmFormulaChains().clear();
									((CommonPlan)o).clearFormulaChains();
								}
							}
						}
					}
					
					EditControlDelegate ed = rtns[i].getEditControlDelegate();
					if (ed != null)
						plans[i].setEditControlDelegate(ed);
					
					BusiObj bo = rtns[i].getBusiObj();
					if (bo != null)
						plans[i].getBusiObj().copyFrom(bo);
				}
			}
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void undoCheckOutPlanByView(Plan so,String pkView) throws BusinessException {
		debug("undo check out "+so.getObjname()+" by view "+pkView);
		try {
			CTLBase.validateISO(so);
			EditControlDelegate ed = EditControlServiceGetter.getIEditControl().undoCheckOutByView(so.getBusiObj(), pkView, so.getEditControlDelegate(), getLoginUserName());
			if (so.isChanged()){
				//so.getActiveModel().unloadCellsList();
				so.fireDataCellChanged();
				if(so instanceof CommonPlan){
					Iterator it = NtbCacheImpl.getCache().listCacheObjects();
					while(it.hasNext()){
						Object o=it.next();
						if(o instanceof CommonPlan){
//							((Plan)o).getHmFormulaChains().clear();
							((CommonPlan)o).clearFormulaChains();
						}
					}
				}
			}
			if (ed != null)
				so.setEditControlDelegate(ed);
			if(so instanceof CommonPlan){
				Plan[] remoteQueryCopy = NtbSheetObjectServiceGetter.getINtbSheetObjectBO().queryByCond(IDimType.PLAN, " pk_obj='" + so.getPrimaryKey() + "'");
				if(remoteQueryCopy != null && remoteQueryCopy.length == 1)
				so.getBusiObj().copyFrom(remoteQueryCopy[0].getBusiObj());
			}else if(so instanceof MetaPlan){
				Plan[] remoteQueryCopy = NtbSheetObjectServiceGetter.getINtbSheetObjectBO().queryByCond(IDimType.STYLESHEET, " pk_obj='" + so.getPrimaryKey() + "'");
				if(remoteQueryCopy != null && remoteQueryCopy.length == 1)
				so.getBusiObj().copyFrom(remoteQueryCopy[0].getBusiObj());
				
			}
			
			//if(!b) expired.
			//ca.invalidate(iso);
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void undoCheckOutPlanArrayByView(CommonPlan[] plans, String[] pk_cubeViews) throws BusinessException {
		if (plans == null || plans.length == 0 || pk_cubeViews == null || pk_cubeViews.length == 0 || pk_cubeViews.length != plans.length)
			return;
		try {
			PlanInfo[] checkedBoList = new PlanInfo[plans.length];
			EditControlDelegate[] checkedEdList = new EditControlDelegate[plans.length];
			for (int i=0; i<plans.length; i++) {
				CTLBase.validateISO(plans[i]);
				checkedBoList[i] = plans[i].getPlanInfo();
				checkedEdList[i] = plans[i].getEditControlDelegate();
			}
			PlanLockRtn[] rtns = EditControlServiceGetter.getIEditControl().undoCheckOutArrayByView(
					checkedBoList, pk_cubeViews, checkedEdList, getLoginUserName());
			if (rtns != null) {
				NtbCacheImpl cache = NtbCacheImpl.getCache();
				for (int i=0; i<rtns.length; i++) {
					if (plans[i].isChanged()) {
						plans[i].fireDataCellChanged();
						if(plans[i] instanceof CommonPlan){
							Iterator it = cache.listCacheObjects();
							while(it.hasNext()){
								Object o=it.next();
								if(o instanceof CommonPlan){
//									((Plan)o).getHmFormulaChains().clear();
									((CommonPlan)o).clearFormulaChains();
								}
							}
						}
					}
					
					EditControlDelegate ed = rtns[i].getEditControlDelegate();
					if (ed != null)
						plans[i].setEditControlDelegate(ed);
					
					BusiObj bo = rtns[i].getBusiObj();
					if (bo != null)
						plans[i].getBusiObj().copyFrom(bo);
				}
			}
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	public static void synEditControlDelegate(Plan[] sos) throws BusinessException {
		try{
			if(sos == null || sos.length == 0)
				return ;			
//			for (int i = 0; i < sos.length; i++) {
//				Plan so = sos[i];
//				EditControlDelegate ecdOld = so.getEditControlDelegate();
//				EditControlDelegate ecd = EditControlServiceGetter.getIEditControl().getLastedEditControlDelegate(so.getBusiObj(), so.getEditControlDelegate());
//				if (ecd != null && ecdOld != null && ecd.getVersionMills() != ecdOld.getVersionMills()){
//					so.unloadCellsList();
//					so.setEditControlDelegate(ecd);
//				}
//			}
			BusiObj[] objs = new BusiObj[sos.length];
			for (int i=0; i<sos.length; i++) {
				objs[i] = sos[i].getBusiObj();
			}
			EditControlDelegate[] eds = EditControlServiceGetter.getIEditControl().getLastedEditControlDelegates(objs);
			if (eds != null) {
				for (int i=0; i<eds.length; i++) {
					sos[i].unloadDataCell();
					sos[i].setEditControlDelegate(eds[i]);
				}
			}
		} catch (BusinessException e) {
			throw e;
		} catch (NtbRException e) {
			throw e;
		} catch (Exception e) {
			NtbLogger.printException(e);
			throw new BusinessException(e.getMessage());
		}
	}
	private static void debug(String s) {
//		System.err.println("[EditControlCTL]: "+s);
	}
}